package com.turing.post.report.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.report.util.CreditDaySettlement;
import com.turing.post.report.util.CreditDaySettlementDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

@Stateless(mappedName = "report.impl.CreditDaySettlementDaoImpl")
@Remote
@Local
public class CreditDaySettlementDaoImpl implements CreditDaySettlementDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void insert(CreditDaySettlement cds) throws PCPSApplicationException {
		GeneralDao.insert(_em, cds);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getCreditDaySettlementReport(String issueCompanyNo,
			String tranDate, Integer page) {
		if (PCPSUtil.isEmpty(issueCompanyNo) || PCPSUtil.isEmpty(tranDate)) {
			return null;
		}
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select t.issuebranchno,t.lastbalance,t.deliver,t.activate,t.cardfee + t.cashfee,t.credit,t.balance, t.capitaldeposit, t.balance+t.credit"
				+ " from creditdaysettlement t"
				+ " where t.issuebranchno like ?1"
				+ " and t.trandate = to_date(?2,'yyyy-MM-dd') order by t.issuebranchno";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueCompanyNo + "%");
		query.setParameter(2, tranDate);
		if (page >= 0) {
			query.setFirstResult(startPosition);
			query.setMaxResults(maxResult);
		}
		return query.getResultList();
	}

	@Override
	public BigInteger getCreditDaySettlementTotalReport(String issueCompanyNo,
			String tranDate) {
		if (PCPSUtil.isEmpty(issueCompanyNo) || PCPSUtil.isEmpty(tranDate)) {
			return null;
		}
		String sql = "select nvl(count(*),0) from creditdaysettlement t"
				+ " where t.issuebranchno like ?1"
				+ " and t.trandate = to_date(?2,'yyyy-MM-dd')";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueCompanyNo + "%");
		query.setParameter(2, tranDate);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void removeCreditDaySettlement(String issueCompanyNo, Date tranDate)
			throws PCPSApplicationException {
		Query query = _em
				.createNativeQuery("delete from creditdaysettlement t "
						+ "where t.issuebranchno like ?1 "
						+ "and t.trandate = ?2");
		query.setParameter(1, issueCompanyNo + "%");
		query.setParameter(2, tranDate);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getCreditDaySettlementByBranch(String issueBranchNo,
			String tranDate) {
		String sql = "select t.issuebranchno,t.lastbalance,t.deliver,t.activate,t.cardfee + t.cashfee,t.credit,t.balance, t.capitaldeposit, t.balance+t.credit"
				+ " from creditdaysettlement t"
				+ " where t.issuebranchno = ?1"
				+ " and t.trandate = to_date(?2,'yyyy-MM-dd') order by t.issuebranchno";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueBranchNo);
		query.setParameter(2, tranDate);
		return query.getResultList();
	}
}
